function form_error(name, id, check, error_str) {
    var inputField = document.getElementById(name);
    var errorMessage = document.getElementById(id);
    if (!check(inputField.value)) {
        inputField.classList.add("error-border");//添加错误样式
        var width = document.getElementsByClassName("required-field")[0].offsetWidth;
        var errorElements = document.getElementsByClassName("form_error");
        for (var i = 0; i < errorElements.length; i++) {
            errorElements[i].style.marginLeft = width + "px";
        }
        errorMessage.textContent = error_str;
        errorMessage.style.display = "block";
        return false; // 阻止表单提交
    } else {
        inputField.classList.remove("error-border");
        errorMessage.style.display = "none";
        return true; // 允许表单提交
    }
}

function form_error_twoinput(name ,name2, id,id2, check, error_str,error_str2) {
    var inputField = document.getElementById(name);
    var inputField2 = document.getElementById(name2);
    var errorMessage = document.getElementById(id);
    var errorMessage2 = document.getElementById(id2);
    if (!check(inputField.value,inputField2.value)) {
        inputField.classList.add("error-border");//添加错误样式
        inputField2.classList.add("error-border");//添加错误样式
        var width = document.getElementsByClassName("required-field")[0].offsetWidth;
        var errorElements = document.getElementsByClassName("form_error");
        for (var i = 0; i < errorElements.length; i++) {
            errorElements[i].style.marginLeft = width + "px";
        }
        errorMessage.textContent = error_str;
        errorMessage.style.display = "block";
        errorMessage2.textContent = error_str2;
        errorMessage2.style.display = "block";
        return false; // 阻止表单提交
    } else {
        inputField.classList.remove("error-border");
        errorMessage.style.display = "none";

        inputField2.classList.remove("error-border");
        errorMessage2.style.display = "none";
        return true; // 允许表单提交
    }
}

function checkNULL(value){
    return value.trim() !== ""
}

function checkLength(value){
    return (value.length>=3&&value.length<=12);
}
function checkNumeric(value) {
    // 使用正则表达式检查字符串是否全为数字
    var regex = /^[0-9]+$/;
    return regex.test(value);
}
function checkPhoneNumber(value) {
    // 使用正则表达式检查字符串是否符合电话号码的格式
    var phoneRegex = /^\d{11}$/;
    return phoneRegex.test(value);
}
function checkValidateDate(value1,value2){
    var tempValue1 = new Date(value1);
    var tempValue2 = new Date(value2);

    return tempValue1<tempValue2;
}
function removeStar(){

}
function validateForm() {

    var error_flag = false;
    var productiondateflag=true;
    var factorysiteflag=true;
    if (!form_error("material_name", "1",checkNULL, "请填写设备名称")) {
        error_flag = true;
    }
    else if(!form_error("material_name", "1", checkLength, "请使字数为3至12位")){
        error_flag = true;
    }
    else{

    }
    if (!form_error("id", "2", checkNULL,"请填写物料编号")) {
        error_flag = true;
    }
    else if (!form_error("id", "2", checkNumeric,"仅填写数字")) {
        error_flag = true;
    }
    if (!form_error("style", "3",checkNULL, "请填写物料类型")) {
        error_flag = true;
    }
    if (!form_error("manufacturer", "4",checkNULL, "请填写生产厂商")) {
        error_flag = true;

    }
    if (!form_error("productiondate", "5",checkNULL, "请填写出厂日期")) {
        error_flag = true;
        productiondateflag=false;
    }

    if (!form_error("factorysite", "6",checkNULL, "请填写厂址")) {
        error_flag = true;
        factorysiteflag=false;
    }
       if(productiondateflag&&factorysiteflag){
        if(form_error_twoinput("productiondate","factorysite","5","6",checkValidateDate,"出厂日期需要比厂址小","厂址需要比出厂日期大")){
             error_flag=false;
        }
    }

    if (!form_error("contacts", "7",checkNULL, "请填写联系人")) {
        error_flag = true;
    }
    if (!form_error("telephone", "8",checkNULL, "请填写联系电话")) {
        error_flag = true;
    }
    else if (!form_error("telephone", "8",checkPhoneNumber, "请填写正确的电话格式")) {
        error_flag = true;
    }

    if(!error_flag){
        window.location.href = "listQuery.jsp";
    }
    return false;
}
